package com.sunyzc.ebaotech.simulator;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class HashUtil {
	private static final int BUFFER_SIZE = 4096;
	/** Message Digest Algorithm (消息摘要算法) 5 */
	private static final String ALGORITHM_MD5 = "MD5";
	/** Secure Hash Algorithm (安全哈希算法) */
	private static final String ALGORITHM_SHA1 = "SHA-1";

	public static String getMD5(File file) throws IOException, NoSuchAlgorithmException {
		return getHashStr(file, ALGORITHM_MD5);
	}

	public static String getSHA1(File file) throws IOException, NoSuchAlgorithmException {
		return getHashStr(file, ALGORITHM_SHA1);
	}

	public static String getMD5(InputStream inputStream) throws IOException, NoSuchAlgorithmException {
		return getHashStr(inputStream, ALGORITHM_MD5);
	}

	public static String getSHA1(InputStream inputStream) throws IOException, NoSuchAlgorithmException {
		return getHashStr(inputStream, ALGORITHM_SHA1);
	}

	public static String getMD5(String strData) throws NoSuchAlgorithmException {
		return getHashStr(strData, ALGORITHM_MD5);
	}

	public static String getSHA1(String strData) throws NoSuchAlgorithmException {
		return getHashStr(strData, ALGORITHM_SHA1);
	}

	public static String getMD5(byte[] byteData) throws NoSuchAlgorithmException {
		return getHashStr(byteData, ALGORITHM_MD5);
	}

	public static String getSHA1(byte[] byteData) throws NoSuchAlgorithmException {
		return getHashStr(byteData, ALGORITHM_SHA1);
	}

	public static String getHashStr(File file, String algorithm) throws IOException, NoSuchAlgorithmException {
		String md5Str = null;
		InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
		try {
			md5Str = getHashStr(inputStream, algorithm);
		} finally {
			IOUtil.closeQuietly(inputStream);
		}
		return md5Str;
	}

	public static String getHashStr(InputStream inputStream, String algorithm) throws IOException, NoSuchAlgorithmException {
		byte[] buffer = new byte[BUFFER_SIZE];
		MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
		int readLength = 0;
		while ((readLength = inputStream.read(buffer)) != -1)
			messageDigest.update(buffer, 0, readLength);
		return parseBytes2HexStr(messageDigest.digest());
	}

	public static String getHashStr(String strData, String algorithm) throws NoSuchAlgorithmException {
		String md5Str = null;
		if (strData != null)
			md5Str = getHashStr(strData.getBytes(), algorithm);
		return md5Str;
	}

	public static String getHashStr(byte[] byteData, String algorithm) throws NoSuchAlgorithmException {
		String md5Str = null;
		if (byteData != null) {
			MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
			messageDigest.update(byteData);
			md5Str = parseBytes2HexStr(messageDigest.digest());
		}
		return md5Str;
	}

	private static String parseBytes2HexStr(byte[] bytes) {
		StringBuffer hexStringBuffer = new StringBuffer(bytes.length << 1);
		for (byte b : bytes) {
			String hexStr = Integer.toHexString(b & 0xFF);
			if (hexStr.length() == 1)
				hexStr = '0' + hexStr;
			hexStringBuffer.append(hexStr.toUpperCase());
		}
		return hexStringBuffer.toString();
	}
}
